package tools.icq.spam;

import core.ProxyConnection;
import java.util.Date;
import core.icq.CoreICQ;


import ru.caffeineim.protocols.icq.contacts.ContactList;
import ru.caffeineim.protocols.icq.core.OscarConnection;
import ru.caffeineim.protocols.icq.integration.OscarInterface;
import ru.caffeineim.protocols.icq.integration.events.ContactListEvent;
import ru.caffeineim.protocols.icq.integration.events.IncomingMessageEvent;
import ru.caffeineim.protocols.icq.integration.events.LoginErrorEvent;
import ru.caffeineim.protocols.icq.integration.events.SsiAuthReplyEvent;
import ru.caffeineim.protocols.icq.integration.events.SsiAuthRequestEvent;
import ru.caffeineim.protocols.icq.integration.events.SsiFutureAuthGrantEvent;
import ru.caffeineim.protocols.icq.integration.events.SsiModifyingAckEvent;
import ru.caffeineim.protocols.icq.integration.events.StatusEvent;
import ru.caffeineim.protocols.icq.setting.enumerations.StatusModeEnum;




/**
 *
 * @author dcrm
 */
public class UinPrime extends CoreICQ {

    private String SERVER = "login.icq.com";
    private int PORT = 5190;
    private String UIN = "";
    private String PASSWORD = "";
    private Thread thread;

    public OscarConnection connection;
    public ProxyConnection proxyConnection;

    //public String MASTER_UIN = "";
    public long TIME_START=0;
    public String WELCOME_MSG="";
    //public boolean F_OF_MD=true;
    //public boolean F_SEND_MSG=true;
    //public boolean F_BACK_MSG=false;
    public boolean F_DEBUG_MSG=true;
    //public boolean F_LOG_MSG=true;
    public boolean F_LIFE=true;
   
    

    /**
     *
     * @param uinChild - UIN
     * @param server - Server Login
     * @param port - Port Login
     * @param proxyConnection - Proxy
     */
    public UinPrime(UinChild uinChild, String server, int port, ProxyConnection proxyConnection) {
        super(uinChild.UIN, uinChild.PWD, server, port, uinChild.UIN, proxyConnection);
        
        this.SERVER=server;
        this.PORT=port;
        this.UIN=uinChild.UIN;
        this.PASSWORD=uinChild.PWD;
        this.proxyConnection=proxyConnection;
        
        //this.connection.addMessagingListener(this);
        //this.connection.addStatusListener(this);
        //this.connection.addObserver(this);
        
        //thread = new Thread(this);
        //thread.setName("Child["+UIN+"]");
        //thread.start();
    }

    /**
     *
     * @param uinChild - UIN
     * @param get - Server Login
     * @param proxyConnection - Proxy
     */
    public UinPrime(UinChild uinChild, ProxyConnection get, ProxyConnection proxyConnection) {
        this(uinChild, get.proxyHost, get.proxyPort, proxyConnection);
    }

    /**
     *
     * @param uinChild - UIN (Login server - Def)
     * @param proxyConnection - Proxy
     */
    public UinPrime(UinChild uinChild, ProxyConnection proxyConnection) {
        this(uinChild, "login.icq.com", 5190, proxyConnection);
    }
/*
    public void sendAuthRequestMessage(String uin, String msg)
    {
        try { ContactList.sendAuthRequestMessage(connection, uin, msg); }
        catch (Exception ex) { ex.printStackTrace(); }
    }

    public void sendAuthReplyMessage(String uin, String msg, boolean auth)
    {
        try { ContactList.sendAuthReplyMessage(connection, uin, msg, auth); }
        catch (Exception ex) { ex.printStackTrace(); }
    }

    public void sendYouWereAdded(String uin)
    {
        try { ContactList.sendYouWereAdded(connection, uin); }
        catch (Exception ex) { ex.printStackTrace(); }
    }
    
    public void removeYourself(String uin)
    {
        try { ContactList.removeYourself(connection, uin); }
        catch (Exception ex) { ex.printStackTrace(); }
    }

    /**
     *
     * @param str - Message
     * @param uin - Uin
     */
    public void sendBasicMessage(String str, String uin) { 
        try { OscarInterface.sendBasicMessage(connection, uin, str);}
        catch (Exception ex) { ex.printStackTrace(); }
    }


    @SuppressWarnings("empty-statement")
    @Override
    public void onIncomingMessage(IncomingMessageEvent e) {
        if (F_DEBUG_MSG) {System.out.println(e.getSenderID() + " sent : " + e.getMessage());};
        try {
        //OscarInterface.sendBasicMessage(connection, MASTER_UIN, "Инфхель Онлайн "+FucLib.getDiffTime(TIME_START, new Date().getTime()));
            //new IncomeMsg(e,this);
            //if (F_BACK_MSG) OscarInterface.sendBasicMessage(this.connection, MASTER_UIN, e.getSenderID().toString()+"->"+e.getMessage().toString());
            //if (F_LOG_MSG) {Main.lr.writeln(e.getSenderID().toString()+"->"+e.getMessage().toString());};
        }
        catch (Exception ex) {System.out.println(ex.getMessage());}
    }

    @SuppressWarnings("empty-statement")
    public void onLogout() {
        try {
                if (F_DEBUG_MSG) { System.out.println("ReConnect #0"); System.out.println("Reconnect on 60 secounds...");};
                Thread.sleep(60000);
                this.reConnect();
        } catch (Exception ex) {
            try {
                if (F_DEBUG_MSG) {System.out.println("ReConnect #1"); System.out.println(ex.getMessage());  System.out.println("Warring! Reconnect on 60 secounds...");}
                Thread.sleep(60000);
                this.reConnect();
            } catch (Exception ex1) { ex1.printStackTrace(); System.out.println("Logged out (possibly due to error)"); }
        }
         if (F_DEBUG_MSG) { System.out.println("ReConnect #X");};
    }

    @SuppressWarnings("empty-statement")
    public void onAuthorizationFailed(LoginErrorEvent e) {
         if (F_DEBUG_MSG) { System.out.println("Authorization Failed! You UIN or Password is not valid");};
    }


    @Override
    public void onLogin() {
        try {
            OscarInterface.changeStatus(this.connection, new StatusModeEnum(StatusModeEnum.INVISIBLE));
            Thread.sleep(3000);
            OscarInterface.changeStatus(this.connection, new StatusModeEnum(StatusModeEnum.EVIL));
            Thread.sleep(3000);
            OscarInterface.changeStatus(this.connection, new StatusModeEnum(StatusModeEnum.FREE_FOR_CHAT));
            // Запросим сообщения, присланные нам в оффлайн
            //OscarInterface.requestOfflineMessages(this.connection);
            Thread.sleep(100);
            this.WELCOME_MSG="Онлайн: "+SERVER+":"+PORT+"; UIN: "+UIN+"; Старт: "+new Date().toLocaleString();
            //OscarInterface.sendBasicMessage(this.connection, MASTER_UIN,WELCOME_MSG);
        } catch (Exception ex) {System.out.println(ex.getMessage());}
    }

    public void onStatusResponse(StatusEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void onUpdateContactList(ContactListEvent event) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void onSsiModifyingAck(SsiModifyingAckEvent event) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void onSsiFutureAuthGrant(SsiFutureAuthGrantEvent event) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void onSsiAuthRequest(SsiAuthRequestEvent event) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void onSsiAuthReply(SsiAuthReplyEvent event) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}